﻿// -----------------------------------------------------------------------
// <copyright file="ResourceUse_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ResourceUse_DAL:BaseDAL
    {

        // Lấy tất cả ResourceUse
        public static List<ResourceUse> GetAll()
        {
            
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.ResourceUses);
            return DB.ResourceUses.ToList();
        }
        // Lấy ResourceUse theo Id
        public static ResourceUse GetDataById(ResourceUse jp)
        {
            return DB.ResourceUses.Where(m => m.Employee_ID == jp.Employee_ID && m.WorkOrder_ID == jp.WorkOrder_ID).FirstOrDefault();
        }
        //Thêm một ResourceUse
        public static bool Insert(ResourceUse ResourceUse)
        {
            try
            {
                DB.ResourceUses.InsertOnSubmit(ResourceUse);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một ResourceUse
        public static bool Delete(ResourceUse ResourceUse)
        {
            try
            {
                DB.ResourceUses.DeleteOnSubmit(ResourceUse);
                DB.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
        public static bool DeleteByID(ResourceUse jp)
        {
            ResourceUse objResourceUse = GetDataById(jp);
            return Delete(objResourceUse);
        }
        // Cập nhật ResourceUse
        public static bool Update(ResourceUse ResourceUse)
        {
            try
            {
                ResourceUse objResourceUse = GetDataById(ResourceUse);
                objResourceUse.WorkOrder_ID = ResourceUse.WorkOrder_ID;
                objResourceUse.Employee_ID = ResourceUse.Employee_ID;
                objResourceUse.Time = ResourceUse.Time;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public static bool isExits(ResourceUse jp)
        {
            ResourceUse objPartJob = null;
            objPartJob = DB.ResourceUses.Where(j => ((j.WorkOrder_ID == jp.WorkOrder_ID) &&
                (j.Employee_ID == jp.Employee_ID))).FirstOrDefault();
            return objPartJob != null ? true : false;
        }
        public static List<ResourceUse> GetDataByWorkOrderID(int id, bool ischanged)
        {
            if (ischanged)
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.ResourceUses);
            }
            return DB.ResourceUses.Where(j => j.WorkOrder_ID == id).ToList();
        }
    }
}
